﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Ahi.Config;
using DevExpress.DXperience.Demos;
using DevExpress.XtraEditors;
using Ahi.Base;
using Ahi.KomisyonEtkinlik;
using Ahi.Toplanti;
using Ahi.IsArama;
using Ahi.PersonelArama;
using Ahi.Raporlama;
using Ahi.Uyelik;

namespace Ahi
{
    public partial class MainForm : Form
    {
        List<Form> acikFormlar = new List<Form>();

        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            TemaKur();
#if DEBUG
            navBarGroupTeknik.Visible = true;
#endif
        }

        private void TemaKur()
        {
            barManager1.ForceInitialize();
            if (barManager1.GetController().PaintStyleName == "Skin")
            {
                menuItemTema.Caption = "Tema: " + DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName;
                menuItemTema.Hint = menuItemTema.Caption;
            }

            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();

            foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins)
            {
                BarButtonItem item = new BarButtonItem(barManager1, cnt.SkinName);
                item.Description = cnt.SkinName;
                menuItemTema.AddItem(item);
                item.ItemClick += new ItemClickEventHandler(item_ItemClick);
            }

            if (!string.IsNullOrEmpty(AppConfig.Current.VarsayilanTema))
            {
                TemaUygula(AppConfig.Current.VarsayilanTema);
            }
        }

        void item_ItemClick(object sender, ItemClickEventArgs e)
        {
            TemaUygula(e.Item.Caption);
        }

        private void TemaUygula(string temaAdi)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(temaAdi);
            barManager1.GetController().PaintStyleName = "Skin";
            menuItemTema.Caption = "Tema: " + temaAdi;
            menuItemTema.Hint = menuItemTema.Caption;

            AppConfig.Current.VarsayilanTema = temaAdi;
            AppConfig.Current.SaveToDatabase();
        }

        private void navItemUyeler_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<UyeListesiForm>();
        }

        private void navItemRaporListesi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<RaporListesiForm>();
        }

        private T ShowForm<T>()
            where T : Form, new()
        {
            var resultForm = (from x in acikFormlar
                              where
                              x is Base.BaseForm &&
                              x.GetType().Name == typeof(T).Name
                              select x).FirstOrDefault();
            if (resultForm != null)
            {
                resultForm.BringToFront();
                return (T)resultForm;
            }

            T frm = new T();
            frm.Show();
            return frm;
        }

        private void navItemRaporOlustur_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<RaporTasarimForm>();
        }

        internal void ChildFormClosed(Base.BaseForm baseForm)
        {
            acikFormlar.Remove(baseForm);
        }

        internal void ChildFormOpened(Base.BaseForm baseForm)
        {
            acikFormlar.Add(baseForm);
        }

        private void navItemToplantiListesi_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<ToplantiListesiForm>();
        }

        private void navItemEtkinlikler_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<EtkinlikTakvimiForm>();
        }

        private void navItemKomisyonlar_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<KomisyonListesiForm>();
        }

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<IsArayanlarForm>();
        }

        private void navBarItem3_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<PersonelArayanlarForm>();
        }

        private void navBarItem3_LinkClicked_1(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<EtkinlikTakvimiForm>();
        }

        private void navBarItem4_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<PersonelArayanlarForm>();
        }

        private void navBarItem6_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<Ahi.Config.AyarlarForm>();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            Program.StopMongoServer();
            Application.Exit();
        }

        private void navItemReset_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Veritabanını sıfırlamak istediğine emin misin?", "Atıyosun", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                MongoAdaptor.Instance.Reset();
        }

        private void navBarItem7_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Program.StartMongoServer();
        }

        private void navBarItem8_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Program.StopMongoServer();
        }

        private void navBarItem9_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            ShowForm<EtkinlikListesiForm>();
        }

        private void buttonHakkinda_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraMessageBox.Show(@"Ahi Dernek Yönetim Bilgi Sistemi
Baran Girişimciler Derneği {www.barander.org} için geliştirilmiştir.

Geliştirici: AdemC
Uygulama Adresi: http://ahi.codeplex.com
İletişim: alfabcsoft@gmail.com", "Ahi Dernek Yönetim Bilgi Sistemi Hakkında", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
